package com.dkd.flow.strategy.service.operation;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dkd.common.core.domain.entity.SysUser;
import com.dkd.flow.context.ProcessContext;
import com.dkd.flow.context.ProcessContextBuilder;
import com.dkd.flow.domain.EProcessInstances;
import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.emums.FlowOperation;
import com.dkd.flow.emums.FlowStatus;
import com.dkd.flow.emums.TaskActionType;
import com.dkd.flow.emums.TaskStatus;
import com.dkd.flow.service.impl.EProcessTaskServiceImpl;
import com.dkd.flow.strategy.annotation.Strategy;
import com.dkd.flow.utils.RevokeUtils;
import com.dkd.flow.utils.TaskCreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Strategy(FlowOperation.REJECT_AND_END)
public class RejectAndEndStrategy extends BaseStrategy {
    @Autowired
    ProcessContextBuilder processContextBuilder;
    @Autowired
    TaskCreate taskCreate;
    @Autowired
    private EProcessTaskServiceImpl eProcessTaskServiceImpl;
    @Autowired
    RevokeUtils revokeUtils;

    @Override
    public Long execute(JSONObject data) {
        // 使用构建器初始化上下文 --- start --- //
        ProcessContext context = processContextBuilder.build(data);
        EProcessInstances eProcessInstances = context.getEProcessInstances();
        EProcessTask curTask = context.getCurTask();
        Long curTaskId = context.getCurTaskId();
        Long curTaskStatus = context.getCurTaskStatus();
        EProcessInstances paramsEProcessInstances = context.getEProcessInstances();
        JSONObject formValue = context.getFormValue();
        Long processDefinitionId = context.getProcessDefinitionId();
        // 使用构建器初始化上下文 --- end --- //
        Long taskGroupId = taskCreate.createAndSetOperationGroupId(eProcessInstances);
        // 放 createEProcessInstances 后面，不然找不到实例id
        initNodeRelations(processDefinitionId);
        // 创建发起人重新发起的任务
        JSONObject launchNode = links.get(0).getJSONObject("toNode");
        if (launchNode == null) {
            throw new RuntimeException("links 中第一个节点的 toNode 为 null，无法创建发起人任务");
        }
        SysUser liableUser = nodeUtils.getLiableUser(eProcessInstances);
        // 创建 审批人任务 APPROVER_TASK 2 自己 已拒绝并关闭 REJECT_AND_END 4 拒绝并结束 REJECT_AND_END 2
        Long approverTaskId = IdUtil.getSnowflakeNextId();
        taskCreate.approverTask(eProcessInstances,
                nodeUtils.getApproverByTask(curTask),
                curTask.getProcessDesignId(),
                TaskStatus.REJECT_AND_END,
                approverTaskId,
                approverTaskId,
                taskGroupId,
                taskGroupId,
                JSONObject.toJSONString(formValue),
                TaskActionType.OPERATION_TASK);
        // 创建 审批人任务 APPROVER_TASK 2 并行 已取消 CANCELED 9 拒绝并结束 REJECT_AND_END 2
        revokeUtils.createSynTasksWithStatus(curTask,
                eProcessInstances,
                TaskStatus.CANCELED,
                approverTaskId,
                taskGroupId,
                formValue,
                TaskActionType.LOG_TASK);
        // 创建 发起人任务 INITIATOR_TASK 1 下一个 已拒绝并关闭 REJECT_AND_END 4 拒绝并结束 REJECT_AND_END 2
        Long launchTaskId = IdUtil.getSnowflakeNextId();
        taskCreate.createLaunchTask(eProcessInstances,
                launchNode,
                null,
                TaskStatus.REJECT_AND_END,
                launchTaskId,
                null,
                approverTaskId,
                liableUser,
                taskGroupId,
                JSONObject.toJSONString(formValue),
                TaskActionType.LOG_TASK);
        // 拒绝并结束 REJECT_AND_END 2
        eProcessInstances.setFlowStatus(FlowStatus.REJECT_AND_END.getValue().longValue());
        updateEProcessInstances(eProcessInstances);
        return curTaskId;
    }
}